/********************************************************************************
Title: Data Preparation for Kenya KLPS Sample
		Define time points
		Define variables to use in each index
		Define survey round to use as reference round
********************************************************************************/

use  "${raw}/kenya_klps/kenya_klps_desc", clear
keep hhid date age female ses
sort hhid date
collapse (firstnm) age ses female, by(hhid)
tempfile desc
save `desc'

use  "${raw}/kenya_klps/kenya_klps.dta", clear
merge m:1 hhid using `desc', nogen


loc ref first == 1

*-------------------------------------------------------------------------------
*			0. Formating and Renaming Variables
*-------------------------------------------------------------------------------

foreach var of varlist female ses {
	gen mi_`var' = mi(`var')
	bysort hhid (mi_`var'): replace `var' = `var'[1] if mi(`var')
}



*			0.1		PID and HHID
*-------------------------------------------------------------------------------
ren hhid pid
tostring pid, gen(hhid)
bysort pid (date): gen first = _n == 1
bysort pid (date): drop if _N == 1

*			0.2		Month
*-------------------------------------------------------------------------------
*gen d = dofc(date)
*drop date 
*ren d date
gen month = month(date) + (year(date) - 2018)*12 - 8 if !mi(date)
tab month
drop if inlist(month, 15,16,17,18)

*-------------------------------------------------------------------------------
*			1. Generating Variable Lists
*-------------------------------------------------------------------------------
macro drop depression  
gl depression depression


replace depression = -1*depression



*-------------------------------------------------------------------------------
*			2. Generating Indices
*-------------------------------------------------------------------------------
foreach g in depression {
	foreach var of varlist $`g' {
	sum `var' if `ref'
	replace `var' = (`var' - `r(mean)')/`r(sd)'
	gen swt_`var' = `var'
}
}

gen ___depression_fw = depression


*-------------------------------------------------------------------------------
*          Unweighted indices
*-------------------------------------------------------------------------------
egen ___depression_nw = rowmean($depression)

*-------------------------------------------------------------------------------
*          Inverse-covariance-weighted indices
*-------------------------------------------------------------------------------
icw_index($depression), gen(___depression_icw)


reg ___depression_nw ses

